<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  fan.php
//  phpweb
//  
//  Created by WillLee on 2011-07-06.
//  Copyright 2011 lishuzu. All rights reserved.
// 
// 
//  pm.php
//  phpweb
//  
//  Created by WillLee on 2011-07-14.
//  Copyright 2011 lishuzu. All rights reserved.
// 
// 短消息
class Pm extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'login', 2);
		}
		
		$this->load->model('user_model');
		$this->load->model('pm_model');
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
		$this->uid = $this->user['uid'];
	}
	
	public function index()
	{
		$data['userinfo'] = $this->user_model->get_user_smart($this->uid);
		$data['self'] = $this->security_model->checkself($this->uid);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($this->uid);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
				// function get_pm_list($uid, $pmnum, $folder, $filter, $start, $ppp = 10) {
		$data['pms'] = $this->pm_model->get_pagepm_list($this->uid);
		realname_get();
		$data['_SN'] = $this->config->item('SN');
		$this->load->template('pm_list_tpl', $data);
	}
	
	// 发送短消息
	public function write(){
		$touid = $this->input->get('touid') ? $this->input->get('touid', true) : '';
		$name = $this->input->get('name') ? $this->input->get('name', true) : '';
		if ($this->user['uid'] == $touid ) {
			showmessage('无法给自己发送T邮','pm/index', 2);
		}
		if (submitcheck('pmsubmit')) {
			$touid = $this->input->post('touid') ? $this->input->post('touid', true) : '';
			$message = $this->input->post('message') ? $this->input->post('message', true) : '';
			if (empty($touid)) {
				showmessage('没有选择收信人' ,'pm/index', 2);
			}
			if (empty($message)) {
				showmessage('写两句T邮内容吧。' ,'pm/write/?'.$touid.'&name='.$name, 2);
			}
			
			$user = array('username'=>$this->user['name'], 'uid' => $this->user['uid']);
			$pmid = $this->pm_model->sendpm('', $message, $user, $touid, 0);
			if ($pmid) {
				showmessage('发送成功','pm/index', 1);
			}else{
				showmessage('发送失败','pm/index', 2);				
			}
		}
		$data['userinfo'] = $this->user_model->get_user_smart($this->uid);
		$data['self'] = $this->security_model->checkself($this->uid);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($this->uid);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		$data['touid'] = $touid;
		$data['toname'] = $name;
		$this->load->template('write_pm_tpl', $data);
	}
	
	// 短消息详情
	public function read(){
		$data = array();
		$pmid = !$this->input->get('pmid') ? 0 : intval($this->input->get('pmid', true));
		$touid = !$this->input->get('touid') ? 0 : intval($this->input->get('touid', true));
		
		
		// 短消息信息
		if ($pmid && $touid) {
			$data['pms'] = $this->pm_model->get_pagepmview_list($this->user['uid'], '0', $touid);
		}else{
			$data['pms']['list'] = $this->pm_model->pm_view($this->user['uid'], $pmid);
			foreach ($data['pms']['list'] as $key => $value) {
				realname_set($value['msgfromid'], $value['msgfrom']);
				$value['avatar_small'] = avatar($value['msgfromid'], 'small');
				$data['pms']['list'][$key] = $value;
			}
			realname_get();
		}
		
		$pmid = empty($data['pms']['list']) ? 0 : @$data['pms'][0]['pmid'];
		$data['pmid'] = $pmid;
		$data['touid'] = $touid;
		
		//用户信息
		$data['userinfo'] = $this->user_model->get_user_smart($this->uid);
		$data['self'] = $this->security_model->checkself($this->uid);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($this->uid);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		
		$data['_SN'] = $this->config->item('SN');
		// 更新session
		$this->security_model->update_user_session();
		
		//点击本页立马更新所有邮件状态
		$this->checknewpm();
		$this->load->template('read_pm_tpl', $data);
	}
	
	// 检查是否有新的消息
	public function checknewpm(){
		//检查当前用户
		if($this->user) {
			$mynewpm = $this->pm_model->check_newpm($this->user['uid']);
			if($this->user['newpm'] != $mynewpm) {
				$this->db->where('uid', $this->user['uid']);
				$this->db->update('user', array('newpm' => $mynewpm));
				// 更新session
				$this->security_model->update_user_session();
			}
		}
		ssetcookie('checkpm', 1, 30);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */